package com.shilox.android.app.logitnow.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.shilox.android.app.logitnow.data.LogMessage;
import com.shilox.android.app.logitnow.sql.DBHelper;

/**
 * TODOs DAO object.
 * 
 * @author itcuties
 *
 */
public class LogMessageDAO {

	private SQLiteDatabase db;
	private DBHelper dbHelper;
	
	public LogMessageDAO(Context context) {
		dbHelper = new DBHelper(context);
		db = dbHelper.getWritableDatabase();
	}
	
	// Close the db
	public void close() {
		db.close();
	}
	
	/**
	 * Create new TODO object
	 * @param todoText
	 */
	public void createLogMessage(String todoText,Integer mood) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("texto", todoText);
		contentValues.put("entryDate", new Date().getTime());
		if(mood!=null && mood>0) {
			contentValues.put("mood", mood);
		}
	    // Insert into DB
		db.insert("log_messages", null, contentValues);
		GregorianCalendar gc = new GregorianCalendar();
		gc.add(GregorianCalendar.DAY_OF_MONTH, -1);
		contentValues.put("entryDate", gc.getTime().getTime());
		contentValues.put("texto", "Prueba 1 día menos");
		db.insert("log_messages", null, contentValues);
		gc.add(GregorianCalendar.DAY_OF_MONTH, -4);
		contentValues.put("entryDate", gc.getTime().getTime());
		contentValues.put("texto", "Prueba 2 día menos");
		db.insert("log_messages", null, contentValues);
	}
	
	/**
	 * Delete TODO object
	 * @param todoId
	 */
	public void deleteLogMessage(int todoId) {
		// Delete from DB where id match
		db.delete("log_messages", "_id = " + todoId, null);
	}
	
	/**
	 * Get all TODOs.
	 * @return
	 */
	public ArrayList<LogMessage> getLogMessages(Long since, Long to, Integer mood, Integer lastDays) {
		ArrayList<LogMessage> logMessagesList = new ArrayList<LogMessage>();
		
		// Name of the columns we want to select
		String[] tableColumns = new String[] {"_id","texto","entryDate","mood"};
		// Query the database
//		String[] selection = new String[]{};
		Cursor cursor;
		if (lastDays!=null && lastDays>0) {
			GregorianCalendar gc = new GregorianCalendar();
			gc.add(GregorianCalendar.DAY_OF_MONTH, -lastDays);
			gc.getTime().getTime();
			String[] selectionValues = new String[]{String.valueOf(gc.getTime().getTime())};
			//cursor = db.query("log_messages", tableColumns, filtro, selectionValues, null, null, "entryDate");
			cursor = db.rawQuery("select _id, texto, entryDate, mood from log_messages where entryDate > ? order by entryDate desc", selectionValues);
		} else {
			cursor = db.query("log_messages", tableColumns, null, null, null, null, "entryDate desc");
			
		}
		
		cursor.moveToFirst();
		Calendar c = null;
		int lastDay = 0;
		int lastMonth = 0;
		int lastYear = 0;
		// Iterate the results
	    while (!cursor.isAfterLast()) {
	    	LogMessage lm = new LogMessage();
	    	// Take values from the DB
	    	lm.setId(cursor.getInt(0));
	    	lm.setTexto(cursor.getString(1));
	    	lm.setEntryDate(new Date(cursor.getLong(2)));
	    	lm.setMood(cursor.getInt(3));
	    	c = Calendar.getInstance();
			c.setTime(new Date(cursor.getLong(2)));
	    	if(lastDay!=c.get(Calendar.DAY_OF_MONTH) || lastMonth!=c.get(Calendar.MONTH) || lastYear!=c.get(Calendar.YEAR)) {
	    		lastDay = c.get(Calendar.DAY_OF_MONTH);
	    		lastMonth = c.get(Calendar.MONTH);
	    		lastYear = c.get(Calendar.YEAR);
	    		LogMessage lmHeader = new LogMessage();
	    		lmHeader.setEntryDate(lm.getEntryDate());
	    		lmHeader.setHeader(true);
	    		logMessagesList.add(lmHeader);
	    	} else {
	    		lm.setHeader(false);
	    	}
	    	// Add to the DB
	    	logMessagesList.add(lm);
	    	
	    	// Move to the next result
	    	cursor.moveToNext();
	    }
		
		return logMessagesList;
	}
	
}
